package com.newsproject.controller;


import com.newsproject.pojo.DTO.OtherUserDTO;
import com.newsproject.pojo.Post;
import com.newsproject.pojo.Result;
import com.newsproject.pojo.User;
import com.newsproject.pojo.vo.UserVo;
import com.newsproject.service.PostService;
import com.newsproject.service.UserService;

import io.swagger.annotations.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
@Api(tags = "个人中心的接口")
public class PersonalController {
    @Autowired
    private PostService postService;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "渲染个人主页",notes = "渲染个人主页")
    @GetMapping("/personal_page")
    @ApiResponses(value = {@ApiResponse(code = 200,
            message = "1.userInfo：用户信息，2.posts：用户的部分帖子",
            response =String.class )})
    public String personalPage(@ApiParam("获取session中userInfo") HttpSession session ,
                               @ApiIgnore RedirectAttributes redirectAttributes,
                               @ApiIgnore Model model) {
        return  userService.redirectToPersonalPage(session,redirectAttributes,model);
    }

    @ApiOperation("查询关注的用户的新闻咨询")
    @GetMapping("/selectFollowerPost")
    @ApiResponses(value = {@ApiResponse(code = 200,
            message = "message:如果没有更多内容了，返回\"没有更多内容了\"" ,
            response = Post.class)}
    )
    @ResponseBody
    public Result<Map<String ,Object>> selectFollowerPost(@ApiIgnore HttpSession session,
                                             @RequestParam(defaultValue = "1") @ApiParam("起始页码") int start ,
                                             @RequestParam(defaultValue = "10") @ApiParam("每页显示的数量") int pageSize) {
        return postService.getFollowerPost(session, start, pageSize);
    }
    @ApiOperation("修改密码")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "1.email：邮箱回显，2.password:密码回显,3.code:验证码回显",response =String.class )})
    @PostMapping("/updatePassword")
    @ResponseBody
    public Result<Object> updatePassword(@RequestParam("email") String email ,
                                         @RequestParam("password")String password,
                                         @RequestParam("code") String code,
                                         HttpSession session
    ){
        return userService.setNewPassword(email, code, password,session);
    }


/*
    @ApiOperation("会员注册接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "注册成功", response = Result.class),
            @ApiResponse(code = 400, message = "参数错误", response = Result.class),
            @ApiResponse(code = 404, message = "用户不存在", response = Result.class),
            @ApiResponse(code = 500, message = "服务器内部错误", response = Result.class)
    })
    @PostMapping("/registerMember")
    public Result<User> registerMember(@RequestBody User request) {
       return userService.registerMember(request);
    }
*/

    @ApiOperation("申请认证")
    @PostMapping("/applyForVerification")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "1.message：提示信息",response =String.class )})
    @ResponseBody
    public Result<Object> applyForVerification(@ApiParam("申请信息")@RequestParam("applyInformation") String applyInformation, HttpSession session){
        return  userService.applyForVerification(session,applyInformation);
    }



    @ApiOperation(value = "更新个人信息",notes = "传递user的信息")
    @PostMapping(value = "/updateUser",consumes = "application/json")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回的是新的user", response = User.class)})
    @ResponseBody
    public ResponseEntity<UserVo> updateUser(@RequestBody UserVo user , @ApiIgnore HttpSession session) throws Exception {
        return userService.updateUser(user,session);
    }

    @ApiOperation(value = "查询粉丝列表",notes = "返回粉丝的userId,用户名，头像")
    @ApiResponses(value = {
                    @ApiResponse(code = 100,
                            message = " boolean  hasMore :  是否有下一页， followerList : 粉丝列表",
                            response = OtherUserDTO.class
                    )
            }
    )
    @GetMapping("/followerList")
    @ResponseBody
    public Result<Map<String ,Object>> followerList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            HttpSession session) {
        return postService.followerList(pageNum,session);
    }


    @ApiOperation(value = "查询关注列表")
    @ApiResponses(value = {
            @ApiResponse(code = 100,
                    message = " boolean  hasMore :  是否有下一页， followedList : 关注列表",
                    response = OtherUserDTO.class
            )
    })
    @GetMapping("/followedList")
    @ResponseBody
    public Result<Map<String ,Object>> followedList(
            @RequestParam(defaultValue = "1") Integer pageNum
            ,HttpSession session) {
        return postService.followedList(pageNum,session);
    }
    @ApiOperation(value = "查询点赞新闻列表",notes = "返回新闻的标题，封面")
    @GetMapping("/heartList")
    @ResponseBody
    public Result<List<Post>> heartList(@ApiIgnore HttpSession session)
    {
        return postService.HeartList(session);
    }
    @ApiOperation(value = "查询收藏新闻的列表",notes = "返回新闻的标题，封面")
    @GetMapping("/bookmarkList")
    @ResponseBody
    public Result<List<Post>> bookmarkList(HttpSession session)
    {
        return postService.BookmarkList(session);
    }

}
